#include "pch.h"
#include "./Tool/Base.h"
#include <time.h>
#include "Api.h"

char AZTable[62] = {
	0
};

void initTable() {
	int k = 0;
	for (char i = 'A'; i <= 'Z'; i++, k++) {
		AZTable[k] = i;
	}
	for (char i = 'a'; i <= 'z'; i++, k++) {
		AZTable[k] = i;
	}
	for (char i = '0'; i <= '9'; i++, k++) {
		AZTable[k] = i;
	}
}



char* GetRandName() {
	static char* name = NULL;
	if (name) {
		return name;
	}
	initTable();
	name = (char*) malloc(20);
	memset(name, 0, 20);

	time_t t = time(NULL);
	srand(t);

	int len = (rand() % 10) + 5;
	
	for (int i = 0; i < len; i++) {
		int index = rand() % sizeof(AZTable);
		name[i] = AZTable[index];
	}

	strcat(name, ".sys");
	return name;

}


char* GetRandServiceName() {
	static char* name = NULL;
	if (name) {
		return name;
	}
	initTable();
	name = (char*)malloc(10);
	memset(name, 0, 20);

	time_t t = time(NULL);
	srand(t);

	int len = (rand() % 4) + 5;

	for (int i = 0; i < len; i++) {
		int index = rand() % sizeof(AZTable);
		name[i] = AZTable[index];
	}
	return name;
}


EXTERN_C BOOLEAN WINAPI SH_Driver_Load() {

	LoadDriver Load;
	char bufPath[MAX_PATH] = { 0 };
	GetTempPathA(MAX_PATH, bufPath);
	
	char* driverName = GetRandName();
	char* serviceName = GetRandServiceName();
	strcat(bufPath, driverName);
	return Load.installDriver(bufPath, serviceName);
}

EXTERN_C VOID WINAPI SH_Driver_UnLoad() {
	
	LoadDriver Load;
	char* serviceName = GetRandServiceName();
	Load.unload(serviceName);
}


EXTERN_C ULONG64 WINAPI SH_Driver_Get_Module_Info(HANDLE pid,char* moduleName) {
	
	return DriverGetModuleInfo(pid, moduleName);

}

ULONG64 WINAPI SH_Driver_Read_Process_Test_Million(HANDLE pid, PVOID address, PVOID buffer, SIZE_T size)
{
	DWORD beginTime = GetTickCount();
	for (int i = 0; i < 1000000; i++) {
		DriverReadProcessMemory(pid, address, buffer, size);
	}
	DWORD endTime = GetTickCount();


	return (endTime - beginTime) / 1000;
}

EXTERN_C BOOLEAN WINAPI SH_Driver_Read_Process_Memory(HANDLE pid,PVOID address,PVOID buffer,SIZE_T size) {



	return DriverReadProcessMemory(pid, address,buffer,size);

}

EXTERN_C BOOLEAN WINAPI SH_Driver_Write_Process_Memory(HANDLE pid, PVOID address, PVOID buffer, SIZE_T size) {


	return DriverWriteProcessMemory(pid, address, buffer, size);

}

BOOLEAN __stdcall SH_Driver_Query_Process_Memory(HANDLE pid, PVOID address, PMEMORY_BASIC_INFORMATION memoryInfo)
{
	return DriverQueryProcessMemory(pid, address, memoryInfo);
}

BOOLEAN __stdcall SH_Driver_Protect_Process(HANDLE pid)
{
	return DriverProtectProcess(pid);
}

BOOLEAN __stdcall SH_Driver_REMOTE_CALL(HANDLE pid, PVOID shellCode, SIZE_T shellCodeSize)
{
	return DriverRemoteCall( pid,  shellCode,  shellCodeSize);
}
